﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Test710.Models;
using PagedList;

namespace Test710.Controllers
{
    public class StadiumController : Controller
    {
        private FmisEntities db = new FmisEntities();

        //
        // GET: /Stadium/

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            var stadium = from s in db.Stadiums select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                stadium = stadium.Where(s => s.StadiumName.ToUpper().Contains(searchString.ToUpper()));
                                      
            }
            switch (sortOrder)
            {
                case "Name_desc":
                    stadium = stadium.OrderByDescending(s => s.StadiumName);
                    break;
                case "Country":
                    stadium = stadium.OrderBy(s => s.Country);
                    break;
                case "Date_desc":
                    stadium = stadium.OrderByDescending(s => s.Country);
                    break;
                default:
                    stadium = stadium.OrderBy(s => s.StadiumName);
                    break;
            }
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            return View(stadium.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Stadium/Details/5

        public ActionResult Details(int id = 0)
        {
            Stadium stadium = db.Stadiums.Find(id);
            if (stadium == null)
            {
                return HttpNotFound();
            }
            return View(stadium);
        }

        //
        // GET: /Stadium/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Stadium/Create

        [HttpPost]
        public ActionResult Create(Stadium stadium)
        {
            if (ModelState.IsValid)
            {
                db.Stadiums.Add(stadium);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(stadium);
        }

        //
        // GET: /Stadium/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Stadium stadium = db.Stadiums.Find(id);
            if (stadium == null)
            {
                return HttpNotFound();
            }
            return View(stadium);
        }

        //
        // POST: /Stadium/Edit/5

        [HttpPost]
        public ActionResult Edit(Stadium stadium)
        {
            if (ModelState.IsValid)
            {
                db.Entry(stadium).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(stadium);
        }

        //
        // GET: /Stadium/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Stadium stadium = db.Stadiums.Find(id);
            if (stadium == null)
            {
                return HttpNotFound();
            }
            return View(stadium);
        }

        //
        // POST: /Stadium/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Stadium stadium = db.Stadiums.Find(id);
            db.Stadiums.Remove(stadium);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}